package com.j.thetabk;

import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.mtp.MtpDevice;
import android.mtp.MtpDeviceInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TransferActivity extends AppCompatActivity {


    public static MtpDevice mtpDevice=null;

    private TextView tv=null;

    private ToggleButton tbImage=null;
    private ToggleButton tbVideo=null;
    private ToggleButton tbMove=null;

    private Button btnStart=null;

    private EditText edFolder=null;

    private String bkFolder=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView);

        tbImage=(ToggleButton)findViewById(R.id.toggleButton);
        tbVideo=(ToggleButton)findViewById(R.id.toggleButton2);
        tbMove=(ToggleButton)findViewById(R.id.toggleButton3);

        btnStart=(Button)findViewById(R.id.button);
        edFolder=(EditText)findViewById(R.id.editText);

        tv.setVisibility(View.VISIBLE);
        tv.setText("storage: "+ MTPTransfer.GetSDCardInfo());
        edFolder.setText(Environment.getExternalStorageDirectory().getPath());

        bkFolder=getPreferences(Context.MODE_PRIVATE).getString("sDir", "/mnt/sdcard/thetaSbracket");

        mtpDevice=GetDevice(getIntent());

        if(mtpDevice==null)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                finishAffinity();
            }

        MtpDeviceInfo di=mtpDevice.getDeviceInfo();
        setTitle(di.getModel()+" "+di.getSerialNumber()+ "(" +di.getVersion()+")");


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                TransferSetup setup=new TransferSetup(tbMove.isChecked(),tbImage.isChecked(),tbVideo.isChecked(),edFolder.getText().toString(),bkFolder);

                Intent startIntent = new Intent(getApplicationContext(),MTPTransferService.class);


                Bundle bundle = new Bundle();
                bundle.putSerializable("setup", setup);
                startIntent.putExtras(bundle);

                startService(startIntent);


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    finishAffinity();
                }

            }
        });
    }


    public MtpDevice GetDevice(Intent intent)
    {
        UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        /*
         * do nothing if UsbDevice instance isn't in the intent.
         */
        if (device != null) {
            /*
             * acquire UsbDeviceConnection instance
             */
            UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
            UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(device);

            /*
             * create MtpDevice instance and open it
             */
            MtpDevice mtpDevice = new MtpDevice(device);
            if (mtpDevice.open(usbDeviceConnection)) {
                return mtpDevice;
            }
        }
        return null;
    }


}
